package com.cool.modules.finance.service.family.impl;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.cool.core.base.BaseServiceImpl;
import com.cool.modules.base.entity.sys.BaseSysUserEntity;
import com.cool.modules.finance.dto.UserFamilyListRequest;
import com.cool.modules.finance.dto.UserFamilyRequest;
import com.cool.modules.finance.dto.UserFamilyVo;
import com.cool.modules.finance.entity.category.FinanceCategoryEntity;
import com.cool.modules.finance.entity.family.FinanceFamilyEntity;
import com.cool.modules.finance.entity.family.FinanceUserFamilyEntity;
import com.cool.modules.finance.mapper.family.FinanceFamilyMapper;
import com.cool.modules.finance.mapper.family.FinanceUserFamilyMapper;
import com.cool.modules.finance.service.family.FinanceFamilyService;
import com.cool.modules.finance.service.family.FinanceUserFamilyService;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.cool.modules.base.entity.sys.table.BaseSysUserEntityTableDef.BASE_SYS_USER_ENTITY;
import static com.cool.modules.finance.entity.family.table.FinanceUserFamilyEntityTableDef.FINANCE_USER_FAMILY_ENTITY;

@Service
@RequiredArgsConstructor
public class FinanceFamilyServiceImpl extends BaseServiceImpl<FinanceFamilyMapper, FinanceFamilyEntity> implements FinanceFamilyService {


    final FinanceUserFamilyMapper userFamilyMapper;

    final FinanceUserFamilyService userFamilyService;

    @Override
    public Long add(FinanceFamilyEntity entity) {
        if (null==entity.getCode()){
            entity.setCode(PinyinUtil.getFirstLetter(entity.getName(), ""));
        }
        return super.add(entity);
    }

    @Override
    public Object addUserFamily(UserFamilyRequest body) {
        //一个用户只能属于一个家庭
        body.getUserIdList().forEach(userId->{
            userFamilyMapper.deleteByQuery(new QueryWrapper().eq(FinanceUserFamilyEntity::getUserId,userId));
        });
        //批量插入
        List<FinanceUserFamilyEntity> userFamilyList =body.getUserIdList().stream().map(x->new FinanceUserFamilyEntity(x, body.getFamilyId())).toList();
        userFamilyMapper.insertBatch(userFamilyList);
        return true;
    }

    @Override
    public List<UserFamilyVo> listUserFamily(UserFamilyListRequest body) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select(FINANCE_USER_FAMILY_ENTITY.ID,BASE_SYS_USER_ENTITY.HEAD_IMG, BASE_SYS_USER_ENTITY.USERNAME, BASE_SYS_USER_ENTITY.NAME, BASE_SYS_USER_ENTITY.NICK_NAME);
        queryWrapper.from(FINANCE_USER_FAMILY_ENTITY).leftJoin(BASE_SYS_USER_ENTITY)
                .on(FINANCE_USER_FAMILY_ENTITY.USER_ID.eq(BASE_SYS_USER_ENTITY.ID));
        queryWrapper.eq(FinanceUserFamilyEntity::getFamilyId,body.getFamilyId());
        if (StringUtils.isNotBlank(body.getKeyWord())){
            queryWrapper.and(qw->{
                qw.where(BaseSysUserEntity::getUsername).like(body.getKeyWord()).
                        or(BaseSysUserEntity::getName).like(body.getKeyWord()).
                        or(BaseSysUserEntity::getNickName).like(body.getKeyWord());
            });
        }
        return userFamilyService.listAs(queryWrapper, UserFamilyVo.class);
    }

    @Override
    public Object deleteUserFamily(Long[] ids) {
        return userFamilyService.delete(ids);
    }

}
